﻿#include <algorithm>
#include <iostream>
#include <string.h>

#include "format/yaml.hpp"

void test1() {
    cppc::yaml::YamlNode root;
    try {
        cppc::yaml::ParseFile(root, "data/test.yaml");
    } catch (std::runtime_error e) {
        std::cout << e.what() << std::endl; // 加载异常
        return;
    }
    std::cout << "----检查----" << std::endl;
    std::cout << std::boolalpha << "has none: " << root.hasSubNode("none") << std::endl;
    std::cout << std::boolalpha << "has a: " << root.hasSubNode("a") << std::endl;
    std::cout << std::boolalpha << "has c.c1: " << root.hasSubNode("c.c1", true) << std::endl;
    std::cout << std::boolalpha << "has d.d1.d12: " << root.hasSubNode("d.d1.d12", true) << std::endl;

    std::cout << "----取值----" << std::endl;
    // 对象取值
    // 指定默认值，并获返回值是否是传入的默认值
    auto aaPair = root["aa"].asStringPair("aaaaaa");
    std::cout << "aa,useDefaultValue: " << aaPair.first << ",getValue: " << aaPair.second << std::endl;

    std::cout << "a-str: " << root["a"].asString() << std::endl;
    std::cout << "a-int: " << root["a"].asInt() << std::endl;
    std::cout << std::boolalpha << "b: " << root["b"].asBool() << std::endl;
    std::cout << std::boolalpha << "b2: " << root["b2"].asBool() << std::endl;
    std::cout << "bbb: " << root["bbb"].asString("bbbbbb") << std::endl;
    std::cout << "d.d1.d12: " << root.getSubNode("d.d1.d12", true).asString("d.d1.d12") << std::endl;
    std::cout << "d.d1.d12: " << root["d"]["d1"]["d12"].asString("d.d1.d12") << std::endl;

    // 数组取值
    cppc::yaml::YamlNode &c1 = root["c"]["c1"];
    std::cout << "c1.size: " << c1.size() << std::endl;
    for (size_t i = 0; i < c1.size(); i++) { std::cout << "  c1[" << i << "]: " << c1[i].asInt() << std::endl; }
    // 数组取值结束

    // 流式取值开始
    cppc::yaml::YamlNode &e = root["e"];
    std::cout << "e.size: " << e.size() << std::endl;
    for (size_t i = 0; i < e.size(); i++) {
        cppc::yaml::YamlNode &eSub = e[i];
        for (size_t k = 0; k < eSub.size(); k++) { std::cout << "  e[" << i << "][" << k << "]: " << eSub[k].asDouble() << std::endl; }
    }

    cppc::yaml::YamlNode &f = root["f"];
    std::cout << "f.size: " << f.size() << std::endl;
    std::vector<std::string> fKeys = f.getSubNodeKeyList();

    for (size_t i = 0; i < fKeys.size(); i++) {
        cppc::yaml::YamlNode &fSub = f[fKeys[i]];
        if (fSub.isScalar()) {
            std::cout << "  f[\"" << fKeys[i] << "\"]: " << fSub.asString() << std::endl;
        }
    }
    // 流式取值结束

    // 赋值
    root["a"] = 456;

    // 添加新值
    cppc::yaml::YamlNode &version = root.pushFront();
    version.setNodeKey("root");
    version = ("1.0.0");
    version.setComment("版本号");

    // 添加新数组
    cppc::yaml::YamlNode &seq1 = root["seq1"];
    seq1.initSequence(); // 新加对象作为数组
    seq1[0] = 11;
    seq1[1] = 22;

    // 添加新流式数组
    cppc::yaml::YamlNode &seq2 = root["seq2"];
    seq2.initSequence(); // 新加对象作为数组
    seq2.setIsFlow(true);
    seq2[0] = 11;
    seq2[1] = 22;
    cppc::yaml::YamlNode &seq2Before = root.insertBefore(seq2);
    seq2Before.initEmptyLine();
    seq2Before.setComment("seq2的注释");

    // 文本插入空行
    root.pushEmptyLine();
    cppc::yaml::YamlNode &emptyLine = root.pushBack(false);
    emptyLine.initEmptyLine();

    // 文本插入空行，并设置注释内容
    emptyLine = root.pushBack(false);
    emptyLine.initEmptyLine();
    emptyLine.setComment("最后一行注释");

    try {
        cppc::yaml::SerializeToFile(root, "data/test_new.yaml");
    } catch (std::runtime_error e) {
        std::cout << e.what() << std::endl; // 序列化至文件异常
        return;
    }
}
int main(int argc, char *argv[]) {
    test1();
    return 0;
}
